package org.epam.szhuk.greenway.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.epam.szhuk.greenway.model.GeoDataCurrentPos;
import org.epam.szhuk.greenway.model.StoredGeoPoint;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
/**
 * 
 * @author Sergey
 *
 */
public final class GeoDataProcessor {
	static final String TAG="GeoDataProcessor";
	public static final int TIME_MS_ROBUST_SAME_TIME=1000; //1000 ms 
	//TODO: /?/ add milliseconds to filenames to more clear calcs;
	
	/**
	 * Search for closest from right or 0 element of storedGeoDataList to the pos
	 * 
	 * @param StoredGeoDataList
	 * @param timeMsec
	 * @return 
	 */
	private GeoDataProcessor(){
		
	}
	public static GeoDataCurrentPos findPos(List<StoredGeoPoint> storedGeoDataList, long timeMsec){
		GeoDataCurrentPos result=null;
		if((storedGeoDataList.size()==0)||(timeMsec-storedGeoDataList.get(0).getTime()>timeMsec)){
			result= new GeoDataCurrentPos(0, 0);
		}
		else{
			for(int i=1; i<storedGeoDataList.size();i++){
				if(storedGeoDataList.get(i).getTime()>timeMsec){
					result= new GeoDataCurrentPos(i-1, timeMsec-storedGeoDataList.get(i-1).getTime());
					break;
				}
			}
		}
		if(result==null){ //set the last pos
			result= new GeoDataCurrentPos(storedGeoDataList.size()-1, 0); //-2 because it will be moved to the next coord in seekTo method
		}
		return result;
	}
	
	/**
	 * Have to be called after init of customVideoView
	 * 
	 * @param videoFilePath
	 * @return
	 */
	public static List<StoredGeoPoint> getGeoPointsForFile(Context context, String storedCoordFilepath){
		
		TextParcer textParcer=new TextParcer();
		List<StoredGeoPoint> stGeoData=null;
		try {
			//String storedCoordFilepath=FileProcessor.getStoredCoordFilepath(videoFilePath);
			stGeoData=textParcer.parseFile(storedCoordFilepath/*,fileCreationTime,fileDuration*/);
			if(stGeoData!=null){
				Log.i(TAG, "StoredGeoPoint List size: "+stGeoData.size());
			}else{
				Log.w(TAG, "stGeoData is null for file: "+storedCoordFilepath);
			}
		} catch (Exception e) {
			Log.e(TAG, "Exception on parsing video coordinates", e); //e.getMessage() could be null here
			return null;
		}
		return stGeoData;
	}	
	
	/**
	 * @param speedFilePath
	 * @return List<StoredGeoPoint>
	 */
	/*public static List<StoredGeoPoint> getGeoPointsForSpeedFile(Context context, String speedFilePath){
		int slashPos=speedFilePath.lastIndexOf(File.separator);
		int dotPos=speedFilePath.lastIndexOf(".");
		if((slashPos>-1)&&(dotPos>-1)){
			String videoFileName=speedFilePath.substring(slashPos+1, dotPos);
			Preferences prefs=new Preferences(context);
			String dirPath=Environment.getExternalStorageDirectory()+File.separator+prefs.getAppFilesFolder();
			String videoFilePath=dirPath+File.separator+videoFileName+".mp4";
			return getGeoPointsForSpeedFile(context, videoFilePath);
		}else{
			Log.e(TAG, "Bad filename: "+speedFilePath);
			return new ArrayList<StoredGeoPoint>();
		}
	}	*/
}
